package controladores;

import java.util.ArrayList;
import java.util.Calendar;

import modelo.GestaoAplicacao;

import com.example.rm.teste.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;

import android.content.DialogInterface;
import android.content.Intent;

import android.view.Menu;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class NovoUtilizador extends MenuPrincipal {
	
	private TextView mDateDisplay;
	
	 private int mYear;
	 private int mMonth;
	 private int mDay;
	 
	 static final int DATE_DIALOG_ID = 1;
	
	public static final String LISTACLIENTE = "ListaDecliente";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_novo_utilizador);
		
		mDateDisplay = (TextView) findViewById(R.id.dataNascimentoDisplay);
        ImageButton pickDate = (ImageButton) findViewById(R.id.imageButtonNUDataNasc);
        pickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        updateDisplay();
        
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.novo_utilizador, menu);
		return true;
	}
	
	public void onClickCancelar(View v){
		Intent intent1 = new Intent(getApplicationContext(), GerirUtilizadores.class);
		startActivity(intent1);
	}
	
	public void onClickOK(View v){
		EditText t = (EditText)findViewById(R.id.editTextNUUtlizador);
		String name = t.getText().toString(); 
		
		TextView tData = (TextView)findViewById(R.id.dataNascimentoDisplay);
		String dataN = tData.getText().toString();
		
		RadioGroup sex = (RadioGroup)findViewById(R.id.radioGroupNUSexo);
		int sexo = sex.getCheckedRadioButtonId();
		String radioButtonSelected = "";
		 
//		switch (sexo) {
//		  case R.id.radioNUF : radioButtonSelected = "Fem";
//		       break;
//		  case R.id.radioNUM: radioButtonSelected = "Mas";
//			   break;
//		}
		
		if(sexo == R.id.radioNUF) 
			radioButtonSelected = "Fem";
		else
			radioButtonSelected = "Mas";
		
		//ArrayList<String> li = new ArrayList<String>();
		
		//Utilizador u = new Utilizador(name, dataN, dg);
		//GestaoAplicacao ga = new GestaoAplicacao();
		estruturasLer.gravarUtilizadores(name,dataN,radioButtonSelected, getApplicationContext());
		estruturasLer.lerUtilizadoresBD(getApplicationContext());
		
		AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);

		dlgAlert.setMessage("Registo adicionado com sucesso.");
		dlgAlert.setTitle("Sucesso");
		dlgAlert.setPositiveButton("OK", 
				new DialogInterface.OnClickListener() {
	        public void onClick(DialogInterface dialog, int which) {
	        	finish(); 
	          }
	      });
		dlgAlert.setCancelable(true);
		dlgAlert.create().show();
				
	}
	
	protected Dialog onCreateDialog(int id) {
        switch (id) {
 
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this,
                mDateSetListener,
                mYear, mMonth, mDay);
        }
        
        return null;
    }
	
	 protected void onPrepareDialog(int id, Dialog dialog) {
	        switch (id) {
	 
	        case DATE_DIALOG_ID:
	            ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
	            break;
	        }
	    }    
	    private void updateDisplay() {
	        mDateDisplay.setText(
	            new StringBuilder()
	            // Month is 0 based so add 1
	            .append(mDay).append("-")
	            .append(mMonth + 1).append("-") 
	            .append(mYear).append(" "));
	    }
	    private DatePickerDialog.OnDateSetListener mDateSetListener =
	        new DatePickerDialog.OnDateSetListener() {
	 
	        public void onDateSet(DatePicker view, int year, int monthOfYear,
	                int dayOfMonth) {
	            mYear = year;
	            mMonth = monthOfYear;
	            mDay = dayOfMonth;
	            updateDisplay();
	        }
	    };

}
